package com.project.structures.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import com.project.configuration.ConfigurationProvider;
import com.project.structures.json.FeaturesMap;

import flexjson.JSONDeserializer;

public class JSONUtils {
	
	public static void marshalToFile(Object object) {
		
		File file = new File(ConfigurationProvider.getInstance().getConfiguration().getProperty("file.features.path"));
		Writer writer = null;
        try {
        	writer = new FileWriter(file);
            writer.write(object.toString());
            
        } catch (IOException e) {
            //TODO LOG
        } finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (Exception e) {
				//LOG
			}
		}
	}
	
	public static Object unmarshalFromFile(String fileXml) {
		BufferedReader in;
		File file = new File(fileXml);
		String json = "";
        try {
            in = new BufferedReader(new FileReader(file));
            while (in.ready()) {
                json += in.readLine();
            }
            in.close();

            return new JSONDeserializer<FeaturesMap>().deserialize(json, FeaturesMap.class);

        } catch (FileNotFoundException e) {
            //TODO LOG
        } catch (IOException e) {
        	//TODO LOG
        } catch (Exception e) {
        	e.printStackTrace();
        }

        return null;
	}
	
}
